package com.koicarp.university.graduate.web.controller;

import com.koicarp.university.graduate.auth.service.ISysUserService;
import com.koicarp.university.graduate.service.vo.userManage.UserRoleVo;
import com.koicarp.university.graduate.auth.annotation.OperLog;
import com.koicarp.university.graduate.auth.constant.ModuleNameEnum;
import com.koicarp.university.graduate.auth.constant.OperateLogTypeEnum;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.dto.SysUserDto;
import com.koicarp.university.graudate.common.entity.SysUser;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.vo.UserByRoleVo;
import com.koicarp.university.graudate.common.vo.UserPasswordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

/**
 * 【用户管理：用户表】(SysUser)表控制层
 *
 * @author liutao
 * @since 2020-09-03 19:53:54
 */
@Slf4j
@RestController
@RequestMapping("sysUser")
@RequiredArgsConstructor
@Api(tags = "用户管理")
public class SysUserController {
    /**
     * 服务对象
     */

    @Autowired
    private ConsumerTokenServices consumerTokenServices;

    private final ISysUserService sysUserService;

    @PostMapping("loginOut")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.LOGOUT,operDesc = "系统用户注销")
    public UniversityResponse loginOut(HttpServletRequest request) throws Exception {
        String authorization = request.getHeader("Authorization");
        String token = StringUtils.replace(authorization, "Bearer ", "");
        System.out.println(token);
        UniversityResponse response = new UniversityResponse();
        if (!consumerTokenServices.revokeToken(token)) {
            log.error("退出登录失败！！！");
            throw new Exception("退出登录失败");
        }
        response.code(BaseConstant.USER_LOGOUT_SUCESS_8888);
        response.message("用户登出成功！！！");
        return response;
    }
    @PostMapping("user")
//    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.READ,operDesc = "用户个人信息")
    public UniversityResponse currentUser(Principal principal) {
        String name = principal.getName();
        System.out.println(principal.getName());
        SysUserDto user = sysUserService.getUserByName(name);
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.data(user);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        return response;
    }
    @PostMapping("getUserListByRole")
    @ApiOperation(value = "根据管理权限查询用户",notes = "根据管理权限查询用户，带分页，和单个查询")
    public UniversityResponse getUserListByRole(@RequestBody UserByRoleVo userByRoleVo){
        UniversityResponse response=null;
        try{
            if (userByRoleVo.getSort().equals("+id")){
                userByRoleVo.setSort("asc");
            }else{
                userByRoleVo.setSort("desc");
            }
            response = sysUserService.getUserListByRoleWithPage(userByRoleVo);
        }catch (Exception e){
            log.error("查询失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
    @PostMapping("removeUserOne")
    @ApiOperation(value = "删除一个用户",notes = "删除一个用户")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.DELETE,operDesc = "删除一个用户")
    public UniversityResponse removeUserOne(@RequestBody SysUser sysUser){
        UniversityResponse response=null;
        try{
            response = sysUserService.removeUserOne(sysUser);
        }catch (Exception e){
            log.error("删除失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("removeUserList")
    @ApiOperation(value = "删除用户",notes = "批量删除用户（根据usrId）")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.DELETE,operDesc = "批量删除用户")
    public UniversityResponse removeUserList(@RequestBody List<String> list){
        UniversityResponse response=null;
        try{
            response = sysUserService.removeUserList(list);
        }catch (Exception e){
            log.error("批量删除失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("resetPassword")
    @ApiOperation(value = "重置密码",notes = "重置密码（根据usrId）")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.DELETE,operDesc = "重置了一个用户密码")
    public UniversityResponse resetPassword(@RequestBody SysUser sysUser){
        UniversityResponse response=null;
        try{
            response = sysUserService.resetPassword(sysUser);
        }catch (Exception e){
            log.error("重置失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("saveUserInfo")
    @ApiOperation(value = "添加用户",notes = "添加用户usrId必须唯一")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.CREATE,operDesc = "添加用户")
    public UniversityResponse saveUserInfo(@RequestBody UserRoleVo userRoleVo){
        UniversityResponse response=null;
        try{
            response = sysUserService.insert(userRoleVo);
        }catch (Exception e){
            log.error("增加失败!!",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("modifyUserInfo")
    @ApiOperation(value = "修改用户信息",notes = "用户usrId不可修改")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.UPDATE,operDesc = "修改用户信息")
    public UniversityResponse modifyUserInfo(@RequestBody SysUser sysUser){
        UniversityResponse response=null;
        try{
            response = sysUserService.update(sysUser);
        }catch (Exception e){
            log.error("修改失败！",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
    @PostMapping("modifyUserInfoWithRole")
    @ApiOperation(value = "修改用户信息和角色信息",notes = "用户usrId不可修改")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.UPDATE,operDesc = "修改用户信息和角色信息")
    public UniversityResponse modifyUserInfoWithRole(@RequestBody UserRoleVo userRoleVo){
        UniversityResponse response=null;
        try{
            response = sysUserService.updateByUser(userRoleVo);
        }catch (Exception e){
            log.error("修改失败！",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("modifyPassword")
    @ApiOperation(value = "修改密码",notes = "用户usrId不可修改")
    @OperLog(moduleName = ModuleNameEnum.UserManage,type = OperateLogTypeEnum.UPDATE,operDesc = "修改密码")
    public UniversityResponse modifyPassword(@RequestBody UserPasswordVo userPasswordVo){
        UniversityResponse response=null;
        try{
            response = sysUserService.updatePassword(userPasswordVo);
        }catch (Exception e){
            log.error("修改失败！",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

}